import TdButton from '@/components/td-button/index.vue'
import {
  COMPANY_PRODUCT_LIST,
  CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT,
  DETAILED_LIST_OF_COMPANY_INDICATORS,
  ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE,
  ENERGY_CONSUMPTION_STRUCTURE_TABLE,
  ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE,
  PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE,
  TABLE_ACCOUNT_CONFIGS,
  TABLE_FIELD_QUARTER,
  TABLE_FIELD_YEAR,
  TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT,
  TABLE_REJECTED_TEXT,
  TABLE_REPORTED_TEXT,
  TABLE_RESOLVED_TEXT,
  TABLE_UNDER_REPORT_TEXT
} from '@/config'
import { loadReportingSituationList } from '@/api/enterpriseQuarterlyReport'

export default {
  components: { TdButton },
  data () {
    return {
      loading: false,
      lists: [],
      columns: [
        {
          label: '企业名称',
          prop: 'name',
          width: 240
        },
        {
          label: TABLE_ACCOUNT_CONFIGS.COMPANY_PRODUCT_LIST.NAME,
          prop: 'table6',
          align: 'center',
          tableAccountId: TABLE_ACCOUNT_CONFIGS.COMPANY_PRODUCT_LIST.ID,
        },
        {
          label: TABLE_ACCOUNT_CONFIGS.DETAILED_LIST_OF_COMPANY_INDICATORS.NAME,
          prop: 'table7',
          align: 'center',
          tableAccountId: TABLE_ACCOUNT_CONFIGS.DETAILED_LIST_OF_COMPANY_INDICATORS.ID,
        },
        {
          label: TABLE_ACCOUNT_CONFIGS.ENERGY_CONSUMPTION_STRUCTURE_TABLE.NAME,
          prop: 'table5',
          align: 'center',
          tableAccountId: TABLE_ACCOUNT_CONFIGS.ENERGY_CONSUMPTION_STRUCTURE_TABLE.ID,
        },
        {
          label: TABLE_ACCOUNT_CONFIGS.ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE.NAME,
          prop: 'table8',
          align: 'center',
          tableAccountId: TABLE_ACCOUNT_CONFIGS.ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE.ID,
        },
        {
          label: TABLE_ACCOUNT_CONFIGS.TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT.NAME,
          prop: 'table2',
          align: 'center',
          tableAccountId: TABLE_ACCOUNT_CONFIGS.TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT.ID,
        },
        // {
        //   label: TABLE_ACCOUNT_CONFIGS.PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE.NAME,
        //   prop: 'table1',
        //   align: 'center',
        //   tableAccountId: TABLE_ACCOUNT_CONFIGS.PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE.ID,
        // },

        // {
        //   label: TABLE_ACCOUNT_CONFIGS.CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT.NAME,
        //   prop: 'table3',
        //   align: 'center',
        //   tableAccountId: TABLE_ACCOUNT_CONFIGS.CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT.ID,
        // },
        // {
        //   label: TABLE_ACCOUNT_CONFIGS.ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE.NAME,
        //   prop: 'table4',
        //   align: 'center',
        //   tableAccountId: TABLE_ACCOUNT_CONFIGS.ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE.ID,
        // },

      ],

      enterpriseId: null,
      enterpriseName: null,
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    },
    enterpriseList () {
      return this.$store.state.energyEnterpriseList
    },
  },
  mounted () {
    this.queryLists()
    this.$store.dispatch('GET_ENTERPRISE_LIST')
  },
  methods: {
    statusText (s) {
      // ’‘、未上报、未上报、已上报、已通过、已退回
      const status = ['', TABLE_UNDER_REPORT_TEXT, TABLE_UNDER_REPORT_TEXT, TABLE_REPORTED_TEXT, TABLE_RESOLVED_TEXT, TABLE_REJECTED_TEXT]
      return status[s] || status[0]
    },
    linkType (s) {
      // ’‘、未上报、暂存、已上报、已通过、已退回
      const types = ['warning', 'warning', 'warning', 'default', 'success', 'danger']
      return types[s] || types[0]
    },
    queryLists () {
      this.loading = true
      const params = {
        [TABLE_FIELD_YEAR]: this.year,
        [TABLE_FIELD_QUARTER]: this.quarter,
        tenantId: String((this.enterprise || this.enterpriseId || '')),
      }
      const getStatus = (list, id) => {
        return (list.find(v => Number(v.id) === Number(id)) || {}).state
      }
      return loadReportingSituationList(params).then(res => {
        if (res.code === 200) {
          res.data = res.data || []
          /**
           * Iterator
           * @param v{{id?: string, list: [], reportingName?: string; [propName]: string}}
           * @returns {{table5: *, table4: *, table3: *, table2: *, table1: *, name: *, id, table8: *, table7: *, table6: *}}
           */
          const mapFn = (v) => ({
            id: v.id,
            name: v.reportingName,
            ['table' + PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE.ID]: getStatus(v.list, PROGRESS_ENERGY_SAVING_TARGET_COMPLETION_TABLE.ID),
            ['table' + TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT.ID]: getStatus(v.list, TABLE_OF_COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT.ID),
            ['table' + CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT.ID]: getStatus(v.list, CONDITION_TABLE_OF_MAJOR_ENERGY_CONSUMING_EQUIPMENT.ID),
            ['table' + ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE.ID]: getStatus(v.list, ENERGY_SAVING_RENOVATION_PROJECT_INFORMATION_TABLE.ID),
            ['table' + ENERGY_CONSUMPTION_STRUCTURE_TABLE.ID]: getStatus(v.list, ENERGY_CONSUMPTION_STRUCTURE_TABLE.ID),
            ['table' + COMPANY_PRODUCT_LIST.ID]: getStatus(v.list, COMPANY_PRODUCT_LIST.ID),
            ['table' + DETAILED_LIST_OF_COMPANY_INDICATORS.ID]: getStatus(v.list, DETAILED_LIST_OF_COMPANY_INDICATORS.ID),
            ['table' + ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE.ID]: getStatus(v.list, ENERGY_CONSUMPTION_STRUCTURE_SUB_TABLE.ID),
          })
          this.lists = res.data.map(mapFn)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    rowStyle ({ row }) {
      const status = Number(row.status)
      if (status === 4) {
        return { background: 'rgba(103, 194, 58, .2)' }
      }
      if (status === 5) {
        return { background: 'rgba(245, 108, 108, .2)' }
      }
      if (status === 1) {
        return { background: 'rgba(192, 196, 204, .1)' }
      }
      return {}
    },
    findTable (id) {
      const tables = Object.keys(TABLE_ACCOUNT_CONFIGS)
      const key = tables.find(key => TABLE_ACCOUNT_CONFIGS[key].ID === Number(id))
      return key ? TABLE_ACCOUNT_CONFIGS[key] : null
    },
    edit (row, tableAccountId) {
      // console.log('tableAccountId: ', tableAccountId)
      const TABLE_ACCOUNT = this.findTable(tableAccountId)
      if (TABLE_ACCOUNT === null) {
        console.log(`tableAccountId is ${tableAccountId}`)
        return false
      }

      const tableStatus = row['table' + tableAccountId]
      this.tableStatus = tableStatus ? String(tableStatus) : ''
      this.tableAccountId = Number(tableAccountId)
      this.enterpriseId = String(row.id)
      this.enterpriseName = row.name
      this.tableName = TABLE_ACCOUNT.NAME
      this.tableColumns = TABLE_ACCOUNT.COLUMNS
      this.$nextTick(() => {
        this.showTableAccount = true
      })
    },
  },
}
